package com.zmkj.cn.comm.vo;


import com.alibaba.fastjson.JSONObject;
import com.zmkj.cn.comm.constant.ResultCode;
import com.zmkj.cn.comm.listener.InitDataListener;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Map;

/**
 * @author 伍绍宇
 * 公共返回对象
 */
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class CommonResultVo<T> {
    /**
     * 错误编号
     */
    private long code;
    /**
     * 错误信息
     */
    private String msg;
    /**
     * 参数
     */
    private T body;
    /**
     * 版本号
     */
    private Map<String, String> edition;

    public CommonResultVo(long code, String msg, T body) {
        this.code = code;
        this.msg = msg;
        this.body = body;
        this.edition = InitDataListener.editions;
    }


    /**
     * 成功返回结果
     *
     * @param body 获取的数据
     */
    public static <T> CommonResultVo<T> success(T body) {
        return new CommonResultVo<T>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg(), body);
    }

    public static <T> CommonResultVo<T> successSocket(T body) {
        return new CommonResultVo<T>(ResultCode.SUCCESS_SOCKET.getCode(), ResultCode.SUCCESS_SOCKET.getMsg(), body);
    }

    /**
     * 成功返回结果
     *
     * @param body 获取的数据
     * @param msg  提示信息
     */
    public static <T> CommonResultVo<T> success(T body, String msg) {
        return new CommonResultVo<T>(ResultCode.SUCCESS.getCode(), msg, body);
    }

    /**
     * 失败返回结果
     *
     * @param errorCode 错误码
     */
    public static <T> CommonResultVo<T> failed(IErrorCode errorCode) {
        return new CommonResultVo<T>(errorCode.getCode(), errorCode.getMsg(), null);
    }

    /**
     * 失败返回结果
     *
     * @param errorCode 错误码
     * @param msg       错误信息
     */
    public static <T> CommonResultVo<T> failed(IErrorCode errorCode, String msg) {
        return new CommonResultVo<T>(errorCode.getCode(), msg, null);
    }

    /**
     * 失败返回结果
     *
     * @param msg 提示信息
     */
    public static <T> CommonResultVo<T> failed(String msg) {
        return new CommonResultVo<T>(ResultCode.FAILED.getCode(), msg, null);
    }

    /**
     * 失败返回结果
     */
    public static <T> CommonResultVo<T> failed() {
        return failed(ResultCode.FAILED);
    }

    /**
     * 参数验证失败返回结果
     */
    public static <T> CommonResultVo<T> validateFailed() {
        return failed(ResultCode.VALIDATE_FAILED);
    }

    /**
     * 参数验证失败返回结果
     *
     * @param msg 提示信息
     */
    public static <T> CommonResultVo<T> validateFailed(String msg) {
        return new CommonResultVo<T>(ResultCode.VALIDATE_FAILED.getCode(), msg, null);
    }

    /**
     * 未登录返回结果
     */
    public static <T> CommonResultVo<T> unauthorized(T body) {
        return new CommonResultVo<T>(ResultCode.UNAUTHORIZED.getCode(), ResultCode.UNAUTHORIZED.getMsg(), body);
    }

    /**
     * 未授权返回结果
     */
    public static <T> CommonResultVo<T> forbidden(T body) {
        return new CommonResultVo<T>(ResultCode.FORBIDDEN.getCode(), ResultCode.FORBIDDEN.getMsg(), body);
    }

    @Override
    public String toString() {
        return JSONObject.toJSONString(this);
    }
}